import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Input } from 'antd';

class WrapSearch extends Component {
  static propTypes = {
    value: PropTypes.string,
    onSearch: PropTypes.func
  };

  constructor(props) {
    super(props);
    this.state = {
      value: props.value || ''
    };
  }

  componentDidUpdate(prevProps) {
    if (prevProps.value !== this.props.value) {
      this.setState({ value: this.props.value });
    }
  }

  onSearch = () => {
    this.props.onSearch(this.state.value.trim());
  };

  render() {
    const { value } = this.state;
    return (
      <Input.Search
        className="search-input"
        enterButton
        value={value}
        onChange={e => this.setState({ value: e.target.value })}
        placeholder="请输入关键词搜索"
        onSearch={this.onSearch}
      />
    );
  }
}

export default WrapSearch;
